home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2002 November / SGI Freeware 2002 November - Disc 1.iso / dist / fw_emacs-lisp-intro.idb / usr / freeware / info / emacs-lisp-intro.info-7.z / emacs-lisp-intro.info-7
Text File  |  2002-07-08  |  48KB  |  1,199 lines

  1. This is emacs-lisp-intro.info, produced by makeinfo version 4.0b from
  2. emacs-lisp-intro.texi.
  3.  
  4. INFO-DIR-SECTION Emacs
  5. START-INFO-DIR-ENTRY
  6. * Emacs Lisp Intro: (eintr).
  7.               A simple introduction to Emacs Lisp programming.
  8. END-INFO-DIR-ENTRY
  9.  
  10.    This is an introduction to `Programming in Emacs Lisp', for people
  11. who are not programmers.
  12.  
  13.    Edition 2.04, 2001 Dec 17
  14.  
  15.    Copyright (C) 1990, '91, '92, '93, '94, '95, '97, 2001 Free Software
  16. Foundation, Inc.
  17.  
  18.    Permission is granted to copy, distribute and/or modify this document
  19. under the terms of the GNU Free Documentation License, Version 1.1 or
  20. any later version published by the Free Software Foundation; with the
  21. Invariant Section being the Preface, with the Front-Cover Texts being
  22. no Front-Cover Texts, and with the Back-Cover Texts being no Back-Cover
  23. Texts.  A copy of the license is included in the section entitled "GNU
  24. Free Documentation License".
  25.  
  26. 
  27. File: emacs-lisp-intro.info,  Node: kill-new function,  Prev: kill-append function,  Up: copy-region-as-kill body
  28.  
  29. The `kill-new' function
  30. .......................
  31.  
  32.    The `kill-new' function looks like this:
  33.  
  34.      (defun kill-new (string &optional replace)
  35.        "Make STRING the latest kill in the kill ring.
  36.      Set the kill-ring-yank pointer to point to it.
  37.      If `interprogram-cut-function' is non-nil, apply it to STRING.
  38.      Optional second argument REPLACE non-nil means that STRING will replace
  39.      the front of the kill ring, rather than being added to the list."
  40.        (and (fboundp 'menu-bar-update-yank-menu)
  41.             (menu-bar-update-yank-menu string (and replace (car kill-ring))))
  42.        (if (and replace kill-ring)
  43.            (setcar kill-ring string)
  44.          (setq kill-ring (cons string kill-ring))
  45.          (if (> (length kill-ring) kill-ring-max)
  46.              (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))
  47.        (setq kill-ring-yank-pointer kill-ring)
  48.        (if interprogram-cut-function
  49.            (funcall interprogram-cut-function string (not replace))))
  50.  
  51.    As usual, we can look at this function in parts.
  52.  
  53.    The first line of the documentation makes sense:
  54.  
  55.      Make STRING the latest kill in the kill ring.
  56.  
  57. Let's skip over the rest of the documentation for the moment.
  58.  
  59.    Also, let's skip over the first two lines of code, those involving
  60. `menu-bar-update-yank-menu'.  We will explain them below.
  61.  
  62.    The critical lines are these:
  63.  
  64.        (if (and replace kill-ring)
  65.            ;; then
  66.            (setcar kill-ring string)
  67.          ;; else
  68.          (setq kill-ring (cons string kill-ring))
  69.          (if (> (length kill-ring) kill-ring-max)
  70.              ;; avoid overly long kill ring
  71.              (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil)))
  72.        (setq kill-ring-yank-pointer kill-ring)
  73.        (if interprogram-cut-function
  74.            (funcall interprogram-cut-function string (not replace))))
  75.  
  76.    The conditional test is `(and replace kill-ring)'.  This will be
  77. true when two conditions are met:  the kill ring has something in it,
  78. and the `replace' variable is true.
  79.  
  80.    The `kill-append' function sets `replace' to be true; then, when the
  81. kill ring has at least one item in it, the `setcar' expression is
  82. executed:
  83.  
  84.      (setcar kill-ring string)
  85.  
  86.    The `setcar' function actually changes the first element of the
  87. `kill-ring' list to the value of `string'.  It replaces the first
  88. element.
  89.  
  90.    On the other hand, if the kill ring is empty, or replace is false,
  91. the else-part of the condition is executed:
  92.  
  93.      (setq kill-ring (cons string kill-ring))
  94.      (if (> (length kill-ring) kill-ring-max)
  95.          (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))
  96.  
  97. This expression first constructs a new version of the kill ring by
  98. prepending `string' to the existing kill ring as a new element.  Then
  99. it executes a second `if' clause.  This second `if' clause keeps the
  100. kill ring from growing too long.
  101.  
  102.    Let's look at these two expressions in order.
  103.  
  104.    The `setq' line of the else-part sets the new value of the kill ring
  105. to what results from adding the string being killed to the old kill
  106. ring.
  107.  
  108.    We can see how this works with an example:
  109.  
  110.      (setq example-list '("here is a clause" "another clause"))
  111.  
  112. After evaluating this expression with `C-x C-e', you can evaluate
  113. `example-list' and see what it returns:
  114.  
  115.      example-list
  116.           => ("here is a clause" "another clause")
  117.  
  118. Now, we can add a new element on to this list by evaluating the
  119. following expression:
  120.  
  121.      (setq example-list (cons "a third clause" example-list))
  122.  
  123. When we evaluate `example-list', we find its value is:
  124.  
  125.      example-list
  126.           => ("a third clause" "here is a clause" "another clause")
  127.  
  128. Thus, the third clause was added to the list by `cons'.
  129.  
  130.    This is exactly similar to what the `setq' and `cons' do in the
  131. function.  Here is the line again:
  132.  
  133.      (setq kill-ring (cons string kill-ring))
  134.  
  135.    Now for the second part of the `if' clause.  This expression keeps
  136. the kill ring from growing too long.  It looks like this:
  137.  
  138.      (if (> (length kill-ring) kill-ring-max)
  139.          (setcdr (nthcdr (1- kill-ring-max) kill-ring) nil))
  140.  
  141.    The code checks whether the length of the kill ring is greater than
  142. the maximum permitted length.  This is the value of `kill-ring-max'
  143. (which is 60, by default).  If the length of the kill ring is too long,
  144. then this code sets the last element of the kill ring to `nil'.  It
  145. does this by using two functions, `nthcdr' and `setcdr'.
  146.  
  147.    We looked at `setcdr' earlier (*note `setcdr': setcdr.).  It sets
  148. the CDR of a list, just as `setcar' sets the CAR of a list.  In this
  149. case, however, `setcdr' will not be setting the CDR of the whole kill
  150. ring; the `nthcdr' function is used to cause it to set the CDR of the
  151. next to last element of the kill ring--this means that since the CDR of
  152. the next to last element is the last element of the kill ring, it will
  153. set the last element of the kill ring.
  154.  
  155.    The `nthcdr' function works by repeatedly taking the CDR of a
  156. list--it takes the CDR of the CDR of the CDR ...  It does this N times
  157. and returns the results.
  158.  
  159.    Thus, if we had a four element list that was supposed to be three
  160. elements long, we could set the CDR of the next to last element to
  161. `nil', and thereby shorten the list.
  162.  
  163.    You can see this by evaluating the following three expressions in
  164. turn.  First set the value of `trees' to `(maple oak pine birch)', then
  165. set the CDR of its second CDR to `nil' and then find the value of
  166. `trees':
  167.  
  168.      (setq trees '(maple oak pine birch))
  169.           => (maple oak pine birch)
  170.      
  171.      (setcdr (nthcdr 2 trees) nil)
  172.           => nil
  173.      
  174.      trees
  175.           => (maple oak pine)
  176.  
  177. (The value returned by the `setcdr' expression is `nil' since that is
  178. what the CDR is set to.)
  179.  
  180.    To repeat, in `kill-new', the `nthcdr' function takes the CDR a
  181. number of times that is one less than the maximum permitted size of the
  182. kill ring and sets the CDR of that element (which will be the rest of
  183. the elements in the kill ring) to `nil'.  This prevents the kill ring
  184. from growing too long.
  185.  
  186.    The next to last expression in the `kill-new' function is
  187.  
  188.      (setq kill-ring-yank-pointer kill-ring)
  189.  
  190.    The `kill-ring-yank-pointer' is a global variable that is set to be
  191. the `kill-ring'.
  192.  
  193.    Even though the `kill-ring-yank-pointer' is called a `pointer', it
  194. is a variable just like the kill ring.  However, the name has been
  195. chosen to help humans understand how the variable is used.  The
  196. variable is used in functions such as `yank' and `yank-pop' (*note
  197. Yanking Text Back: Yanking.).
  198.  
  199.    Now, to return to the first two lines in the body of the function:
  200.  
  201.        (and (fboundp 'menu-bar-update-yank-menu)
  202.             (menu-bar-update-yank-menu string (and replace (car kill-ring))))
  203.  
  204. This is an expression whose first element is the function `and'.
  205.  
  206.    The `and' special form evaluates each of its arguments until one of
  207. the arguments returns a value of `nil', in which case the `and'
  208. expression returns `nil'; however, if none of the arguments returns a
  209. value of `nil', the value resulting from evaluating the last argument
  210. is returned.  (Since such a value is not `nil', it is considered true
  211. in Emacs Lisp.)  In other words, an `and' expression returns a true
  212. value only if all its arguments are true.
  213.  
  214.    In this case, the expression tests first to see whether
  215. `menu-bar-update-yank-menu' exists as a function, and if so, calls it.
  216. The `fboundp' function returns true if the symbol it is testing has a
  217. function definition that `is not void'.  If the symbol's function
  218. definition were void, we would receive an error message, as we did when
  219. we created errors intentionally (*note Generate an Error Message:
  220. Making Errors.).
  221.  
  222.    Essentially, the `and' is an `if' expression that reads like this:
  223.  
  224.      if THE-MENU-BAR-FUNCTION-EXISTS
  225.        then EXECUTE-IT
  226.  
  227.    `menu-bar-update-yank-menu' is one of the functions that make it
  228. possible to use the `Select and Paste' menu in the Edit item of a menu
  229. bar; using a mouse, you can look at the various pieces of text you have
  230. saved and select one piece to paste.
  231.  
  232.    Finally, the last expression in the `kill-new' function adds the
  233. newly copied string to whatever facility exists for copying and pasting
  234. among different programs running in a windowing system.  In the X
  235. Windowing system, for example, the `x-select-text' function takes the
  236. string and stores it in memory operated by X.  You can paste the string
  237. in another program, such as an Xterm.
  238.  
  239.    The expression looks like this:
  240.  
  241.        (if interprogram-cut-function
  242.            (funcall interprogram-cut-function string (not replace))))
  243.  
  244.    If an `interprogram-cut-function' exists, then Emacs executes
  245. `funcall', which in turn calls its first argument as a function and
  246. passes the remaining arguments to it.  (Incidentally, as far as I can
  247. see, this `if' expression could be replaced by an `and' expression
  248. similar to the one in the first part of the function.)
  249.  
  250.    We are not going to discuss windowing systems and other programs
  251. further, but merely note that this is a mechanism that enables GNU
  252. Emacs to work easily and well with other programs.
  253.  
  254.    This code for placing text in the kill ring, either concatenated with
  255. an existing element or as a new element, leads us to the code for
  256. bringing back text that has been cut out of the buffer--the yank
  257. commands.  However, before discussing the yank commands, it is better
  258. to learn how lists are implemented in a computer.  This will make clear
  259. such mysteries as the use of the term `pointer'.
  260.  
  261. 
  262. File: emacs-lisp-intro.info,  Node: cons & search-fwd Review,  Next: search Exercises,  Prev: copy-region-as-kill,  Up: Cutting & Storing Text
  263.  
  264. Review
  265. ======
  266.  
  267.    Here is a brief summary of some recently introduced functions.
  268.  
  269. `car'
  270. `cdr'
  271.      `car' returns the first element of a list; `cdr' returns the
  272.      second and subsequent elements of a list.
  273.  
  274.      For example:
  275.  
  276.           (car '(1 2 3 4 5 6 7))
  277.                => 1
  278.           (cdr '(1 2 3 4 5 6 7))
  279.                => (2 3 4 5 6 7)
  280.  
  281. `cons'
  282.      `cons' constructs a list by prepending its first argument to its
  283.      second argument.
  284.  
  285.      For example:
  286.  
  287.           (cons 1 '(2 3 4))
  288.                => (1 2 3 4)
  289.  
  290. `nthcdr'
  291.      Return the result of taking CDR `n' times on a list.  The `rest of
  292.      the rest', as it were.
  293.  
  294.      For example:
  295.  
  296.           (nthcdr 3 '(1 2 3 4 5 6 7))
  297.                => (4 5 6 7)
  298.  
  299. `setcar'
  300. `setcdr'
  301.      `setcar' changes the first element of a list; `setcdr' changes the
  302.      second and subsequent elements of a list.
  303.  
  304.      For example:
  305.  
  306.           (setq triple '(1 2 3))
  307.           
  308.           (setcar triple '37)
  309.           
  310.           triple
  311.                => (37 2 3)
  312.           
  313.           (setcdr triple '("foo" "bar"))
  314.           
  315.           triple
  316.                => (37 "foo" "bar")
  317.  
  318. `progn'
  319.      Evaluate each argument in sequence and then return the value of the
  320.      last.
  321.  
  322.      For example:
  323.  
  324.           (progn 1 2 3 4)
  325.                => 4
  326.  
  327. `save-restriction'
  328.      Record whatever narrowing is in effect in the current buffer, if
  329.      any, and restore that narrowing after evaluating the arguments.
  330.  
  331. `search-forward'
  332.      Search for a string, and if the string is found, move point.
  333.  
  334.      Takes four arguments:
  335.  
  336.        1. The string to search for.
  337.  
  338.        2. Optionally, the limit of the search.
  339.  
  340.        3. Optionally, what to do if the search fails, return `nil' or an
  341.           error message.
  342.  
  343.        4. Optionally, how many times to repeat the search; if negative,
  344.           the search goes backwards.
  345.  
  346. `kill-region'
  347. `delete-region'
  348. `copy-region-as-kill'
  349.      `kill-region' cuts the text between point and mark from the buffer
  350.      and stores that text in the kill ring, so you can get it back by
  351.      yanking.
  352.  
  353.      `delete-and-extract-region' removes the text between point and
  354.      mark from the buffer and throws it away.  You cannot get it back.
  355.  
  356.      `copy-region-as-kill' copies the text between point and mark into
  357.      the kill ring, from which you can get it by yanking.  The function
  358.      does not cut or remove the text from the buffer.
  359.  
  360. 
  361. File: emacs-lisp-intro.info,  Node: search Exercises,  Prev: cons & search-fwd Review,  Up: Cutting & Storing Text
  362.  
  363. Searching Exercises
  364. ===================
  365.  
  366.    * Write an interactive function that searches for a string.  If the
  367.      search finds the string, leave point after it and display a message
  368.      that says "Found!".  (Do not use `search-forward' for the name of
  369.      this function; if you do, you will overwrite the existing version
  370.      of `search-forward' that comes with Emacs.  Use a name such as
  371.      `test-search' instead.)
  372.  
  373.    * Write a function that prints the third element of the kill ring in
  374.      the echo area, if any; if the kill ring does not contain a third
  375.      element, print an appropriate message.
  376.  
  377. 
  378. File: emacs-lisp-intro.info,  Node: List Implementation,  Next: Yanking,  Prev: Cutting & Storing Text,  Up: Top
  379.  
  380. How Lists are Implemented
  381. *************************
  382.  
  383.    In Lisp, atoms are recorded in a straightforward fashion; if the
  384. implementation is not straightforward in practice, it is, nonetheless,
  385. straightforward in theory.  The atom `rose', for example, is recorded
  386. as the four contiguous letters `r', `o', `s', `e'.  A list, on the
  387. other hand, is kept differently.  The mechanism is equally simple, but
  388. it takes a moment to get used to the idea.  A list is kept using a
  389. series of pairs of pointers.  In the series, the first pointer in each
  390. pair points to an atom or to another list, and the second pointer in
  391. each pair points to the next pair, or to the symbol `nil', which marks
  392. the end of the list.
  393.  
  394.    A pointer itself is quite simply the electronic address of what is
  395. pointed to.  Hence, a list is kept as a series of electronic addresses.
  396.  
  397. * Menu:
  398.  
  399. * Lists diagrammed::
  400. * Symbols as Chest::            Exploring a powerful metaphor.
  401. * List Exercise::
  402.  
  403. 
  404. File: emacs-lisp-intro.info,  Node: Lists diagrammed,  Next: Symbols as Chest,  Prev: List Implementation,  Up: List Implementation
  405.  
  406. Lists diagrammed
  407. ================
  408.  
  409.    For example, the list `(rose violet buttercup)' has three elements,
  410. `rose', `violet', and `buttercup'.  In the computer, the electronic
  411. address of `rose' is recorded in a segment of computer memory along
  412. with the address that gives the electronic address of where the atom
  413. `violet' is located; and that address (the one that tells where
  414. `violet' is located) is kept along with an address that tells where the
  415. address for the atom `buttercup' is located.
  416.  
  417.    This sounds more complicated than it is and is easier seen in a
  418. diagram:
  419.  
  420.          ___ ___      ___ ___      ___ ___
  421.         |___|___|--> |___|___|--> |___|___|--> nil
  422.           |            |            |
  423.           |            |            |
  424.            --> rose     --> violet   --> buttercup
  425.  
  426.  
  427.  
  428.  
  429. In the diagram, each box represents a word of computer memory that
  430. holds a Lisp object, usually in the form of a memory address.  The
  431. boxes, i.e. the addresses, are in pairs.  Each arrow points to what the
  432. address is the address of, either an atom or another pair of addresses.
  433. The first box is the electronic address of `rose' and the arrow points
  434. to `rose'; the second box is the address of the next pair of boxes, the
  435. first part of which is the address of `violet' and the second part of
  436. which is the address of the next pair.  The very last box points to the
  437. symbol `nil', which marks the end of the list.
  438.  
  439.    When a variable is set to a list with a function such as `setq', it
  440. stores the address of the first box in the variable.  Thus, evaluation
  441. of the expression
  442.  
  443.      (setq bouquet '(rose violet buttercup))
  444.  
  445. creates a situation like this:
  446.  
  447.      bouquet
  448.           |
  449.           |     ___ ___      ___ ___      ___ ___
  450.            --> |___|___|--> |___|___|--> |___|___|--> nil
  451.                  |            |            |
  452.                  |            |            |
  453.                   --> rose     --> violet   --> buttercup
  454.  
  455.  
  456.  
  457.  
  458. In this example, the symbol `bouquet' holds the address of the first
  459. pair of boxes.
  460.  
  461.    This same list can be illustrated in a different sort of box notation
  462. like this:
  463.  
  464.      bouquet
  465.       |
  466.       |    --------------       ---------------       ----------------
  467.       |   | car   | cdr  |     | car    | cdr  |     | car     | cdr  |
  468.        -->| rose  |   o------->| violet |   o------->| butter- |  nil |
  469.           |       |      |     |        |      |     | cup     |      |
  470.            --------------       ---------------       ----------------
  471.  
  472.  
  473.  
  474.  
  475.    (Symbols consist of more than pairs of addresses, but the structure
  476. of a symbol is made up of addresses.  Indeed, the symbol `bouquet'
  477. consists of a group of address-boxes, one of which is the address of
  478. the printed word `bouquet', a second of which is the address of a
  479. function definition attached to the symbol, if any, a third of which is
  480. the address of the first pair of address-boxes for the list `(rose
  481. violet buttercup)', and so on.  Here we are showing that the symbol's
  482. third address-box points to the first pair of address-boxes for the
  483. list.)
  484.  
  485.    If a symbol is set to the CDR of a list, the list itself is not
  486. changed; the symbol simply has an address further down the list.  (In
  487. the jargon, CAR and CDR are `non-destructive'.)  Thus, evaluation of
  488. the following expression
  489.  
  490.      (setq flowers (cdr bouquet))
  491.  
  492. produces this:
  493.  
  494.  
  495.      bouquet        flowers
  496.        |              |
  497.        |     ___ ___  |     ___ ___      ___ ___
  498.         --> |   |   |  --> |   |   |    |   |   |
  499.             |___|___|----> |___|___|--> |___|___|--> nil
  500.               |              |            |
  501.               |              |            |
  502.                --> rose       --> violet   --> buttercup
  503.  
  504.  
  505.  
  506.  
  507.  
  508. The value of `flowers' is `(violet buttercup)', which is to say, the
  509. symbol `flowers' holds the address of the pair of address-boxes, the
  510. first of which holds the address of `violet', and the second of which
  511. holds the address of `buttercup'.
  512.  
  513.    A pair of address-boxes is called a "cons cell" or "dotted pair".
  514. *Note List Type: (elisp)List Type, and *Note Dotted Pair Notation:
  515. (elisp)Dotted Pair Notation, for more information about cons cells and
  516. dotted pairs.
  517.  
  518.    The function `cons' adds a new pair of addresses to the front of a
  519. series of addresses like that shown above.  For example, evaluating the
  520. expression
  521.  
  522.      (setq bouquet (cons 'lily bouquet))
  523.  
  524. produces:
  525.  
  526.  
  527.      bouquet                       flowers
  528.        |                             |
  529.        |     ___ ___        ___ ___  |     ___ ___       ___ ___
  530.         --> |   |   |      |   |   |  --> |   |   |     |   |   |
  531.             |___|___|----> |___|___|----> |___|___|---->|___|___|--> nil
  532.               |              |              |             |
  533.               |              |              |             |
  534.                --> lily      --> rose       --> violet    --> buttercup
  535.  
  536.  
  537.  
  538.  
  539.  
  540. However, this does not change the value of the symbol `flowers', as you
  541. can see by evaluating the following,
  542.  
  543.      (eq (cdr (cdr bouquet)) flowers)
  544.  
  545. which returns `t' for true.
  546.  
  547.    Until it is reset, `flowers' still has the value `(violet
  548. buttercup)'; that is, it has the address of the cons cell whose first
  549. address is of `violet'.  Also, this does not alter any of the
  550. pre-existing cons cells; they are all still there.
  551.  
  552.    Thus, in Lisp, to get the CDR of a list, you just get the address of
  553. the next cons cell in the series; to get the CAR of a list, you get the
  554. address of the first element of the list; to `cons' a new element on a
  555. list, you add a new cons cell to the front of the list.  That is all
  556. there is to it!  The underlying structure of Lisp is brilliantly simple!
  557.  
  558.    And what does the last address in a series of cons cells refer to?
  559. It is the address of the empty list, of `nil'.
  560.  
  561.    In summary, when a Lisp variable is set to a value, it is provided
  562. with the address of the list to which the variable refers.
  563.  
  564. 
  565. File: emacs-lisp-intro.info,  Node: Symbols as Chest,  Next: List Exercise,  Prev: Lists diagrammed,  Up: List Implementation
  566.  
  567. Symbols as a Chest of Drawers
  568. =============================
  569.  
  570.    In an earlier section, I suggested that you might imagine a symbol as
  571. being a chest of drawers.  The function definition is put in one
  572. drawer, the value in another, and so on.  What is put in the drawer
  573. holding the value can be changed without affecting the contents of the
  574. drawer holding the function definition, and vice-versa.
  575.  
  576.    Actually, what is put in each drawer is the address of the value or
  577. function definition.  It is as if you found an old chest in the attic,
  578. and in one of its drawers you found a map giving you directions to
  579. where the buried treasure lies.
  580.  
  581.    (In addition to its name, symbol definition, and variable value, a
  582. symbol has a `drawer' for a "property list" which can be used to record
  583. other information.  Property lists are not discussed here; see *Note
  584. Property Lists: (elisp)Property Lists.)
  585.  
  586.    Here is a fanciful representation:
  587.  
  588.  
  589.                  Chest of Drawers            Contents of Drawers
  590.      
  591.                  __   o0O0o   __
  592.                /                 \
  593.               ---------------------
  594.              |    directions to    |            [map to]
  595.              |     symbol name     |             bouquet
  596.              |                     |
  597.              +---------------------+
  598.              |    directions to    |
  599.              |  symbol definition  |             [none]
  600.              |                     |
  601.              +---------------------+
  602.              |    directions to    |            [map to]
  603.              |    variable value   |             (rose violet buttercup)
  604.              |                     |
  605.              +---------------------+
  606.              |    directions to    |
  607.              |    property list    |             [not described here]
  608.              |                     |
  609.              +---------------------+
  610.              |/                   \|
  611.  
  612.  
  613.  
  614.  
  615.  
  616. 
  617. File: emacs-lisp-intro.info,  Node: List Exercise,  Prev: Symbols as Chest,  Up: List Implementation
  618.  
  619. Exercise
  620. ========
  621.  
  622.    Set `flowers' to `violet' and `buttercup'.  Cons two more flowers on
  623. to this list and set this new list to `more-flowers'.  Set the CAR of
  624. `flowers' to a fish.  What does the `more-flowers' list now contain?
  625.  
  626. 
  627. File: emacs-lisp-intro.info,  Node: Yanking,  Next: Loops & Recursion,  Prev: List Implementation,  Up: Top
  628.  
  629. Yanking Text Back
  630. *****************
  631.  
  632.    Whenever you cut text out of a buffer with a `kill' command in GNU
  633. Emacs, you can bring it back with a `yank' command.  The text that is
  634. cut out of the buffer is put in the kill ring and the yank commands
  635. insert the appropriate contents of the kill ring back into a buffer
  636. (not necessarily the original buffer).
  637.  
  638.    A simple `C-y' (`yank') command inserts the first item from the kill
  639. ring into the current buffer.  If the `C-y' command is followed
  640. immediately by `M-y', the first element is replaced by the second
  641. element.  Successive `M-y' commands replace the second element with the
  642. third, fourth, or fifth element, and so on.  When the last element in
  643. the kill ring is reached, it is replaced by the first element and the
  644. cycle is repeated.  (Thus the kill ring is called a `ring' rather than
  645. just a `list'.  However, the actual data structure that holds the text
  646. is a list.  *Note Handling the Kill Ring: Kill Ring, for the details of
  647. how the list is handled as a ring.)
  648.  
  649. * Menu:
  650.  
  651. * Kill Ring Overview::          The kill ring is a list.
  652. * kill-ring-yank-pointer::      The `kill-ring-yank-pointer' variable.
  653. * yank nthcdr Exercises::
  654.  
  655. 
  656. File: emacs-lisp-intro.info,  Node: Kill Ring Overview,  Next: kill-ring-yank-pointer,  Prev: Yanking,  Up: Yanking
  657.  
  658. Kill Ring Overview
  659. ==================
  660.  
  661.    The kill ring is a list of textual strings.  This is what it looks
  662. like:
  663.  
  664.      ("some text" "a different piece of text" "yet more text")
  665.  
  666.    If this were the contents of my kill ring and I pressed `C-y', the
  667. string of characters saying `some text' would be inserted in this
  668. buffer where my cursor is located.
  669.  
  670.    The `yank' command is also used for duplicating text by copying it.
  671. The copied text is not cut from the buffer, but a copy of it is put on
  672. the kill ring and is inserted by yanking it back.
  673.  
  674.    Three functions are used for bringing text back from the kill ring:
  675. `yank', which is usually bound to `C-y'; `yank-pop', which is usually
  676. bound to `M-y'; and `rotate-yank-pointer', which is used by the two
  677. other functions.
  678.  
  679.    These functions refer to the kill ring through a variable called the
  680. `kill-ring-yank-pointer'.  Indeed, the insertion code for both the
  681. `yank' and `yank-pop' functions is:
  682.  
  683.      (insert (car kill-ring-yank-pointer))
  684.  
  685.    To begin to understand how `yank' and `yank-pop' work, it is first
  686. necessary to look at the `kill-ring-yank-pointer' variable and the
  687. `rotate-yank-pointer' function.
  688.  
  689. 
  690. File: emacs-lisp-intro.info,  Node: kill-ring-yank-pointer,  Next: yank nthcdr Exercises,  Prev: Kill Ring Overview,  Up: Yanking
  691.  
  692. The `kill-ring-yank-pointer' Variable
  693. =====================================
  694.  
  695.    `kill-ring-yank-pointer' is a variable, just as `kill-ring' is a
  696. variable.  It points to something by being bound to the value of what
  697. it points to, like any other Lisp variable.
  698.  
  699.    Thus, if the value of the kill ring is:
  700.  
  701.      ("some text" "a different piece of text" "yet more text")
  702.  
  703. and the `kill-ring-yank-pointer' points to the second clause, the value
  704. of `kill-ring-yank-pointer' is:
  705.  
  706.      ("a different piece of text" "yet more text")
  707.  
  708.    As explained in the previous chapter (*note List Implementation::),
  709. the computer does not keep two different copies of the text being
  710. pointed to by both the `kill-ring' and the `kill-ring-yank-pointer'.
  711. The words "a different piece of text" and "yet more text" are not
  712. duplicated.  Instead, the two Lisp variables point to the same pieces of
  713. text.  Here is a diagram:
  714.  
  715.      kill-ring     kill-ring-yank-pointer
  716.          |               |
  717.          |      ___ ___  |     ___ ___      ___ ___
  718.           ---> |   |   |  --> |   |   |    |   |   |
  719.                |___|___|----> |___|___|--> |___|___|--> nil
  720.                  |              |            |
  721.                  |              |            |
  722.                  |              |             --> "yet more text"
  723.                  |              |
  724.                  |               --> "a different piece of text
  725.                  |
  726.                   --> "some text"
  727.  
  728.  
  729.  
  730.  
  731.  
  732.    Both the variable `kill-ring' and the variable
  733. `kill-ring-yank-pointer' are pointers.  But the kill ring itself is
  734. usually described as if it were actually what it is composed of.  The
  735. `kill-ring' is spoken of as if it were the list rather than that it
  736. points to the list.  Conversely, the `kill-ring-yank-pointer' is spoken
  737. of as pointing to a list.
  738.  
  739.    These two ways of talking about the same thing sound confusing at
  740. first but make sense on reflection.  The kill ring is generally thought
  741. of as the complete structure of data that holds the information of what
  742. has recently been cut out of the Emacs buffers.  The
  743. `kill-ring-yank-pointer' on the other hand, serves to indicate--that
  744. is, to `point to'--that part of the kill ring of which the first
  745. element (the CAR) will be inserted.
  746.  
  747.    The `rotate-yank-pointer' function changes the element in the kill
  748. ring to which the `kill-ring-yank-pointer' points; when the pointer is
  749. set to point to the next element beyond the end of the kill ring, it
  750. automatically sets it to point to the first element of the kill ring.
  751. This is how the list is transformed into a ring.  The
  752. `rotate-yank-pointer' function itself is not difficult, but contains
  753. many details.  It and the much simpler `yank' and `yank-pop' functions
  754. are described in an appendix.  *Note Handling the Kill Ring: Kill Ring.
  755.  
  756. 
  757. File: emacs-lisp-intro.info,  Node: yank nthcdr Exercises,  Prev: kill-ring-yank-pointer,  Up: Yanking
  758.  
  759. Exercises with `yank' and `nthcdr'
  760. ==================================
  761.  
  762.    * Using `C-h v' (`describe-variable'), look at the value of your
  763.      kill ring.  Add several items to your kill ring; look at its value
  764.      again.  Using `M-y' (`yank-pop)', move all the way around the kill
  765.      ring.  How many items were in your kill ring?  Find the value of
  766.      `kill-ring-max'.  Was your kill ring full, or could you have kept
  767.      more blocks of text within it?
  768.  
  769.    * Using `nthcdr' and `car', construct a series of expressions to
  770.      return the first, second, third, and fourth elements of a list.
  771.  
  772. 
  773. File: emacs-lisp-intro.info,  Node: Loops & Recursion,  Next: Regexp Search,  Prev: Yanking,  Up: Top
  774.  
  775. Loops and Recursion
  776. *******************
  777.  
  778.    Emacs Lisp has two primary ways to cause an expression, or a series
  779. of expressions, to be evaluated repeatedly: one uses a `while' loop,
  780. and the other uses "recursion".
  781.  
  782.    Repetition can be very valuable.  For example, to move forward four
  783. sentences, you need only write a program that will move forward one
  784. sentence and then repeat the process four times.  Since a computer does
  785. not get bored or tired, such repetitive action does not have the
  786. deleterious effects that excessive or the wrong kinds of repetition can
  787. have on humans.
  788.  
  789.    People mostly write Emacs Lisp functions using `while' loops and
  790. their kin; but you can use recursion, which provides a very powerful
  791. way to think about and then to solve problems(1).
  792.  
  793. * Menu:
  794.  
  795. * while::                       Causing a stretch of code to repeat.
  796. * dolist dotimes::
  797. * Recursion::                   Causing a function to call itself.
  798. * Looping exercise::
  799.  
  800.    ---------- Footnotes ----------
  801.  
  802.    (1) You can write recursive functions to be frugal or wasteful of
  803. mental or computer resources; as it happens, methods that people find
  804. easy--that are frugal of `mental resources'--sometimes use considerable
  805. computer resources.  Emacs was designed to run on machines that we now
  806. consider limited and its default settings are conservative.  You may
  807. want to increase the values of `max-specpdl-size' and
  808. `max-lisp-eval-depth'.  In my `.emacs' file, I set them to 15 and 30
  809. times their default value.
  810.  
  811. 
  812. File: emacs-lisp-intro.info,  Node: while,  Next: dolist dotimes,  Prev: Loops & Recursion,  Up: Loops & Recursion
  813.  
  814. `while'
  815. =======
  816.  
  817.    The `while' special form tests whether the value returned by
  818. evaluating its first argument is true or false.  This is similar to what
  819. the Lisp interpreter does with an `if'; what the interpreter does next,
  820. however, is different.
  821.  
  822.    In a `while' expression, if the value returned by evaluating the
  823. first argument is false, the Lisp interpreter skips the rest of the
  824. expression (the "body" of the expression) and does not evaluate it.
  825. However, if the value is true, the Lisp interpreter evaluates the body
  826. of the expression and then again tests whether the first argument to
  827. `while' is true or false.  If the value returned by evaluating the
  828. first argument is again true, the Lisp interpreter again evaluates the
  829. body of the expression.
  830.  
  831.    The template for a `while' expression looks like this:
  832.  
  833.      (while TRUE-OR-FALSE-TEST
  834.        BODY...)
  835.  
  836. * Menu:
  837.  
  838. * Looping with while::          Repeat so long as test returns true.
  839. * Loop Example::                A `while' loop that uses a list.
  840. * print-elements-of-list::      Uses `while', `car', `cdr'.
  841. * Incrementing Loop::           A loop with an incrementing counter.
  842. * Decrementing Loop::           A loop with a decrementing counter.
  843.  
  844. 
  845. File: emacs-lisp-intro.info,  Node: Looping with while,  Next: Loop Example,  Prev: while,  Up: while
  846.  
  847. Looping with `while'
  848. --------------------
  849.  
  850.    So long as the true-or-false-test of the `while' expression returns
  851. a true value when it is evaluated, the body is repeatedly evaluated.
  852. This process is called a loop since the Lisp interpreter repeats the
  853. same thing again and again, like an airplane doing a loop.  When the
  854. result of evaluating the true-or-false-test is false, the Lisp
  855. interpreter does not evaluate the rest of the `while' expression and
  856. `exits the loop'.
  857.  
  858.    Clearly, if the value returned by evaluating the first argument to
  859. `while' is always true, the body following will be evaluated again and
  860. again ... and again ... forever.  Conversely, if the value returned is
  861. never true, the expressions in the body will never be evaluated.  The
  862. craft of writing a `while' loop consists of choosing a mechanism such
  863. that the true-or-false-test returns true just the number of times that
  864. you want the subsequent expressions to be evaluated, and then have the
  865. test return false.
  866.  
  867.    The value returned by evaluating a `while' is the value of the
  868. true-or-false-test.  An interesting consequence of this is that a
  869. `while' loop that evaluates without error will return `nil' or false
  870. regardless of whether it has looped 1 or 100 times or none at all.  A
  871. `while' expression that evaluates successfully never returns a true
  872. value!  What this means is that `while' is always evaluated for its
  873. side effects, which is to say, the consequences of evaluating the
  874. expressions within the body of the `while' loop.  This makes sense.  It
  875. is not the mere act of looping that is desired, but the consequences of
  876. what happens when the expressions in the loop are repeatedly evaluated.
  877.  
  878. 
  879. File: emacs-lisp-intro.info,  Node: Loop Example,  Next: print-elements-of-list,  Prev: Looping with while,  Up: while
  880.  
  881. A `while' Loop and a List
  882. -------------------------
  883.  
  884.    A common way to control a `while' loop is to test whether a list has
  885. any elements.  If it does, the loop is repeated; but if it does not,
  886. the repetition is ended.  Since this is an important technique, we will
  887. create a short example to illustrate it.
  888.  
  889.    A simple way to test whether a list has elements is to evaluate the
  890. list: if it has no elements, it is an empty list and will return the
  891. empty list, `()', which is a synonym for `nil' or false.  On the other
  892. hand, a list with elements will return those elements when it is
  893. evaluated.  Since Emacs Lisp considers as true any value that is not
  894. `nil', a list that returns elements will test true in a `while' loop.
  895.  
  896.    For example, you can set the variable `empty-list' to `nil' by
  897. evaluating the following `setq' expression:
  898.  
  899.      (setq empty-list ())
  900.  
  901. After evaluating the `setq' expression, you can evaluate the variable
  902. `empty-list' in the usual way, by placing the cursor after the symbol
  903. and typing `C-x C-e'; `nil' will appear in your echo area:
  904.  
  905.      empty-list
  906.  
  907.    On the other hand, if you set a variable to be a list with elements,
  908. the list will appear when you evaluate the variable, as you can see by
  909. evaluating the following two expressions:
  910.  
  911.      (setq animals '(gazelle giraffe lion tiger))
  912.      
  913.      animals
  914.  
  915.    Thus, to create a `while' loop that tests whether there are any
  916. items in the list `animals', the first part of the loop will be written
  917. like this:
  918.  
  919.      (while animals
  920.             ...
  921.  
  922. When the `while' tests its first argument, the variable `animals' is
  923. evaluated.  It returns a list.  So long as the list has elements, the
  924. `while' considers the results of the test to be true; but when the list
  925. is empty, it considers the results of the test to be false.
  926.  
  927.    To prevent the `while' loop from running forever, some mechanism
  928. needs to be provided to empty the list eventually.  An oft-used
  929. technique is to have one of the subsequent forms in the `while'
  930. expression set the value of the list to be the CDR of the list.  Each
  931. time the `cdr' function is evaluated, the list will be made shorter,
  932. until eventually only the empty list will be left.  At this point, the
  933. test of the `while' loop will return false, and the arguments to the
  934. `while' will no longer be evaluated.
  935.  
  936.    For example, the list of animals bound to the variable `animals' can
  937. be set to be the CDR of the original list with the following expression:
  938.  
  939.      (setq animals (cdr animals))
  940.  
  941. If you have evaluated the previous expressions and then evaluate this
  942. expression, you will see `(giraffe lion tiger)' appear in the echo
  943. area.  If you evaluate the expression again, `(lion tiger)' will appear
  944. in the echo area.  If you evaluate it again and yet again, `(tiger)'
  945. appears and then the empty list, shown by `nil'.
  946.  
  947.    A template for a `while' loop that uses the `cdr' function
  948. repeatedly to cause the true-or-false-test eventually to test false
  949. looks like this:
  950.  
  951.      (while TEST-WHETHER-LIST-IS-EMPTY
  952.        BODY...
  953.        SET-LIST-TO-CDR-OF-LIST)
  954.  
  955.    This test and use of `cdr' can be put together in a function that
  956. goes through a list and prints each element of the list on a line of its
  957. own.
  958.  
  959. 
  960. File: emacs-lisp-intro.info,  Node: print-elements-of-list,  Next: Incrementing Loop,  Prev: Loop Example,  Up: while
  961.  
  962. An Example: `print-elements-of-list'
  963. ------------------------------------
  964.  
  965.    The `print-elements-of-list' function illustrates a `while' loop
  966. with a list.
  967.  
  968.    The function requires several lines for its output.  If you are
  969. reading this in Emacs 21 or a later version, you can evaluate the
  970. following expression inside of Info, as usual.
  971.  
  972.    If you are using an earlier version of Emacs, you need to copy the
  973. necessary expressions to your `*scratch*' buffer and evaluate them
  974. there.  This is because the echo area had only one line in the earlier
  975. versions.
  976.  
  977.    You can copy the expressions by marking the beginning of the region
  978. with `C-<SPC>' (`set-mark-command'), moving the cursor to the end of
  979. the region and then copying the region using `M-w'
  980. (`copy-region-as-kill').  In the `*scratch*' buffer, you can yank the
  981. expressions back by typing `C-y' (`yank').
  982.  
  983.    After you have copied the expressions to the `*scratch*' buffer,
  984. evaluate each expression in turn.  Be sure to evaluate the last
  985. expression, `(print-elements-of-list animals)', by typing `C-u C-x
  986. C-e', that is, by giving an argument to `eval-last-sexp'.  This will
  987. cause the result of the evaluation to be printed in the `*scratch*'
  988. buffer instead of being printed in the echo area.  (Otherwise you will
  989. see something like this in your echo area:
  990. `^Jgiraffe^J^Jgazelle^J^Jlion^J^Jtiger^Jnil', in which each `^J' stands
  991. for a `newline'.)
  992.  
  993.    If you are using Emacs 21 or later, you can evaluate these
  994. expressions directly in the Info buffer, and the echo area will grow to
  995. show the results.
  996.  
  997.      (setq animals '(gazelle giraffe lion tiger))
  998.      
  999.      (defun print-elements-of-list (list)
  1000.        "Print each element of LIST on a line of its own."
  1001.        (while list
  1002.          (print (car list))
  1003.          (setq list (cdr list))))
  1004.      
  1005.      (print-elements-of-list animals)
  1006.  
  1007. When you evaluate the three expressions in sequence, you will see this:
  1008.  
  1009.      giraffe
  1010.      
  1011.      gazelle
  1012.      
  1013.      lion
  1014.      
  1015.      tiger
  1016.      nil
  1017.  
  1018.    Each element of the list is printed on a line of its own (that is
  1019. what the function `print' does) and then the value returned by the
  1020. function is printed.  Since the last expression in the function is the
  1021. `while' loop, and since `while' loops always return `nil', a `nil' is
  1022. printed after the last element of the list.
  1023.  
  1024. 
  1025. File: emacs-lisp-intro.info,  Node: Incrementing Loop,  Next: Decrementing Loop,  Prev: print-elements-of-list,  Up: while
  1026.  
  1027. A Loop with an Incrementing Counter
  1028. -----------------------------------
  1029.  
  1030.    A loop is not useful unless it stops when it ought.  Besides
  1031. controlling a loop with a list, a common way of stopping a loop is to
  1032. write the first argument as a test that returns false when the correct
  1033. number of repetitions are complete.  This means that the loop must have
  1034. a counter--an expression that counts how many times the loop repeats
  1035. itself.
  1036.  
  1037.    The test can be an expression such as `(< count desired-number)'
  1038. which returns `t' for true if the value of `count' is less than the
  1039. `desired-number' of repetitions and `nil' for false if the value of
  1040. `count' is equal to or is greater than the `desired-number'.  The
  1041. expression that increments the count can be a simple `setq' such as
  1042. `(setq count (1+ count))', where `1+' is a built-in function in Emacs
  1043. Lisp that adds 1 to its argument.  (The expression `(1+ count)' has the
  1044. same result as `(+ count 1)', but is easier for a human to read.)
  1045.  
  1046.    The template for a `while' loop controlled by an incrementing
  1047. counter looks like this:
  1048.  
  1049.      SET-COUNT-TO-INITIAL-VALUE
  1050.      (while (< count desired-number)         ; true-or-false-test
  1051.        BODY...
  1052.        (setq count (1+ count)))              ; incrementer
  1053.  
  1054. Note that you need to set the initial value of `count'; usually it is
  1055. set to 1.
  1056.  
  1057. * Menu:
  1058.  
  1059. * Incrementing Example::        Counting pebbles in a triangle.
  1060. * Inc Example parts::           The parts of the function definition.
  1061. * Inc Example altogether::      Putting the function definition together.
  1062.  
  1063. 
  1064. File: emacs-lisp-intro.info,  Node: Incrementing Example,  Next: Inc Example parts,  Prev: Incrementing Loop,  Up: Incrementing Loop
  1065.  
  1066. Example with incrementing counter
  1067. .................................
  1068.  
  1069.    Suppose you are playing on the beach and decide to make a triangle of
  1070. pebbles, putting one pebble in the first row, two in the second row,
  1071. three in the third row and so on, like this:
  1072.  
  1073.  
  1074.                     *
  1075.                    * *
  1076.                   * * *
  1077.                  * * * *
  1078.  
  1079.  
  1080.  
  1081. (About 2500 years ago, Pythagoras and others developed the beginnings of
  1082. number theory by considering questions such as this.)
  1083.  
  1084.    Suppose you want to know how many pebbles you will need to make a
  1085. triangle with 7 rows?
  1086.  
  1087.    Clearly, what you need to do is add up the numbers from 1 to 7.
  1088. There are two ways to do this; start with the smallest number, one, and
  1089. add up the list in sequence, 1, 2, 3, 4 and so on; or start with the
  1090. largest number and add the list going down: 7, 6, 5, 4 and so on.
  1091. Because both mechanisms illustrate common ways of writing `while'
  1092. loops, we will create two examples, one counting up and the other
  1093. counting down.  In this first example, we will start with 1 and add 2,
  1094. 3, 4 and so on.
  1095.  
  1096.    If you are just adding up a short list of numbers, the easiest way
  1097. to do it is to add up all the numbers at once.  However, if you do not
  1098. know ahead of time how many numbers your list will have, or if you want
  1099. to be prepared for a very long list, then you need to design your
  1100. addition so that what you do is repeat a simple process many times
  1101. instead of doing a more complex process once.
  1102.  
  1103.    For example, instead of adding up all the pebbles all at once, what
  1104. you can do is add the number of pebbles in the first row, 1, to the
  1105. number in the second row, 2, and then add the total of those two rows
  1106. to the third row, 3.  Then you can add the number in the fourth row, 4,
  1107. to the total of the first three rows; and so on.
  1108.  
  1109.    The critical characteristic of the process is that each repetitive
  1110. action is simple.  In this case, at each step we add only two numbers,
  1111. the number of pebbles in the row and the total already found.  This
  1112. process of adding two numbers is repeated again and again until the last
  1113. row has been added to the total of all the preceding rows.  In a more
  1114. complex loop the repetitive action might not be so simple, but it will
  1115. be simpler than doing everything all at once.
  1116.  
  1117. 
  1118. File: emacs-lisp-intro.info,  Node: Inc Example parts,  Next: Inc Example altogether,  Prev: Incrementing Example,  Up: Incrementing Loop
  1119.  
  1120. The parts of the function definition
  1121. ....................................
  1122.  
  1123.    The preceding analysis gives us the bones of our function definition:
  1124. first, we will need a variable that we can call `total' that will be
  1125. the total number of pebbles.  This will be the value returned by the
  1126. function.
  1127.  
  1128.    Second, we know that the function will require an argument: this
  1129. argument will be the total number of rows in the triangle.  It can be
  1130. called `number-of-rows'.
  1131.  
  1132.    Finally, we need a variable to use as a counter.  We could call this
  1133. variable `counter', but a better name is `row-number'.  That is because
  1134. what the counter does is count rows, and a program should be written to
  1135. be as understandable as possible.
  1136.  
  1137.    When the Lisp interpreter first starts evaluating the expressions in
  1138. the function, the value of `total' should be set to zero, since we have
  1139. not added anything to it.  Then the function should add the number of
  1140. pebbles in the first row to the total, and then add the number of
  1141. pebbles in the second to the total, and then add the number of pebbles
  1142. in the third row to the total, and so on, until there are no more rows
  1143. left to add.
  1144.  
  1145.    Both `total' and `row-number' are used only inside the function, so
  1146. they can be declared as local variables with `let' and given initial
  1147. values.  Clearly, the initial value for `total' should be 0.  The
  1148. initial value of `row-number' should be 1, since we start with the
  1149. first row.  This means that the `let' statement will look like this:
  1150.  
  1151.        (let ((total 0)
  1152.              (row-number 1))
  1153.          BODY...)
  1154.  
  1155.    After the internal variables are declared and bound to their initial
  1156. values, we can begin the `while' loop.  The expression that serves as
  1157. the test should return a value of `t' for true so long as the
  1158. `row-number' is less than or equal to the `number-of-rows'.  (If the
  1159. expression tests true only so long as the row number is less than the
  1160. number of rows in the triangle, the last row will never be added to the
  1161. total; hence the row number has to be either less than or equal to the
  1162. number of rows.)
  1163.  
  1164.    Lisp provides the `<=' function that returns true if the value of
  1165. its first argument is less than or equal to the value of its second
  1166. argument and false otherwise.  So the expression that the `while' will
  1167. evaluate as its test should look like this:
  1168.  
  1169.      (<= row-number number-of-rows)
  1170.  
  1171.    The total number of pebbles can be found by repeatedly adding the
  1172. number of pebbles in a row to the total already found.  Since the
  1173. number of pebbles in the row is equal to the row number, the total can
  1174. be found by adding the row number to the total.  (Clearly, in a more
  1175. complex situation, the number of pebbles in the row might be related to
  1176. the row number in a more complicated way; if this were the case, the
  1177. row number would be replaced by the appropriate expression.)
  1178.  
  1179.      (setq total (+ total row-number))
  1180.  
  1181. What this does is set the new value of `total' to be equal to the sum
  1182. of adding the number of pebbles in the row to the previous total.
  1183.  
  1184.    After setting the value of `total', the conditions need to be
  1185. established for the next repetition of the loop, if there is one.  This
  1186. is done by incrementing the value of the `row-number' variable, which
  1187. serves as a counter.  After the `row-number' variable has been
  1188. incremented, the true-or-false-test at the beginning of the `while'
  1189. loop tests whether its value is still less than or equal to the value
  1190. of the `number-of-rows' and if it is, adds the new value of the
  1191. `row-number' variable to the `total' of the previous repetition of the
  1192. loop.
  1193.  
  1194.    The built-in Emacs Lisp function `1+' adds 1 to a number, so the
  1195. `row-number' variable can be incremented with this expression:
  1196.  
  1197.      (setq row-number (1+ row-number))
  1198.  
  1199.